﻿Imports Babok

Partial Public Class CadastroRequisitos
    Inherits BasePage

#Region "Atributos da pagina"

    Public Property ItemPriorizadoSelecionado() As ItemPriorizado
        Get
            Return TryCast(ViewState("_itemPriorizadoSelecionado"), ItemPriorizado)
        End Get
        Set(ByVal value As ItemPriorizado)
            If value IsNot Nothing Then
                ViewState("_itemPriorizadoSelecionado") = value
                ViewState("_itemSelecionado") = value.ItemBackLog
                ViewState("_backlog") = value.Backlog
            End If
            Me.DataBindItemSelecionado()
        End Set
    End Property

    'Public ReadOnly Property ItemSelecionado() As ItemBackLog
    '    Get
    '        Return TryCast(ViewState("_itemSelecionado"), ItemBackLog)
    '    End Get
    'End Property


    Public Property IdItemSelecionado() As Long
        Get
            Return CLng(ViewState("_idItemSelecionado"))
        End Get
        Set(ByVal value As Long)
            ViewState("_idItemSelecionado") = value
        End Set
    End Property


    Public ReadOnly Property BacklogSelecionado() As Backlog
        Get
            Return TryCast(ViewState("_backlog"), Backlog)
        End Get
    End Property

    Public Property IndexGdRequisitoEditar() As Integer
        Get
            If CInt(ViewState("_GdRequisitoEditar")) = 0 Then ViewState("_GdRequisitoEditar") = -1
            Return CInt(ViewState("_GdRequisitoEditar"))
        End Get
        Set(ByVal value As Integer)
            ViewState("_GdRequisitoEditar") = value
            Me.DataBindRequisitos()
        End Set
    End Property

#End Region

#Region "Delegates"

    Protected Delegate Sub PageDelegate()

#End Region

#Region "Classes Injetadas"

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Me.DataBindRequisitos()
        End If

    End Sub

#Region "Funcoes de Databind"

    Protected Sub DataBindRequisitos()

        Dim _item As ItemBackLog
        Dim _requisitos As IList(Of Requisito)

        Me.IdItemSelecionado = 1

        _item = New ItemBackLog
        _item = _item.Buscar(Me.IdItemSelecionado)
        _requisitos = _item.Requisitos

        GdRequisitos.EditIndex = Me.IndexGdRequisitoEditar
        GdRequisitos.DataSource = _requisitos
        GdRequisitos.DataBind()

    End Sub

    Private Sub GdRequisitos_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GdRequisitos.RowCreated

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim BtCancelar As Button = CType(e.Row.FindControl("BtCancelar"), Button)
            Dim LkRequisito As LinkButton = CType(e.Row.FindControl("LkRequisito"), LinkButton)
            Dim BtAlterar As Button = CType(e.Row.FindControl("BtAlterar"), Button)

            If BtCancelar IsNot Nothing Then AddHandler BtCancelar.Click, AddressOf Me.SairEdicao
            If LkRequisito IsNot Nothing Then AddHandler LkRequisito.Click, AddressOf Me.SolicitaEdicao
            If BtAlterar IsNot Nothing Then AddHandler BtAlterar.Click, AddressOf Me.AlterarRequisito

        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            Dim BtSalvar As Button = CType(e.Row.FindControl("BtSalvar"), Button)

            If BtSalvar IsNot Nothing Then AddHandler BtSalvar.Click, AddressOf Me.SalvarNovoRequisito
        End If

    End Sub

    Protected Sub SalvarNovoRequisito(ByVal sender As Button, ByVal e As System.EventArgs)

        Dim _requisito As Requisito
        Dim _row As GridViewRow = GdRequisitos.FooterRow
        Dim TxtReqSalvar As TextBox = CType(_row.FindControl("TxtReqSalvar"), TextBox)
        Dim _descricao As String
        Dim _item As ItemBackLog

        _item = New ItemBackLog
        _item = _item.Buscar(Me.IdItemSelecionado)
        _descricao = TxtReqSalvar.Text

        _requisito = New Requisito(_descricao)
        _requisito.Salvar(_item)

        Me.DataBindRequisitos()

    End Sub

    Protected Sub GdRequisitos_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GdRequisitos.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim _dataItem As Requisito = CType(e.Row.DataItem, Requisito)
            Dim LkRequisito As LinkButton = CType(e.Row.FindControl("LkRequisito"), LinkButton)
            Dim LbRequisito As Label = CType(e.Row.FindControl("LbRequisito"), Label)
            Dim BtAlterar As Button = CType(e.Row.FindControl("BtAlterar"), Button)
            Dim BtCancelar As Button = CType(e.Row.FindControl("BtCancelar"), Button)
            Dim TxtReqAlterar As TextBox = CType(e.Row.FindControl("TxtReqAlterar"), TextBox)

            If e.Row.RowIndex = Me.IndexGdRequisitoEditar Then
                BtAlterar.CommandArgument = _dataItem.Id
                TxtReqAlterar.Text = _dataItem.Descricao
            Else
                LkRequisito.Text = _dataItem.Id
                LkRequisito.CommandArgument = e.Row.RowIndex
                LbRequisito.Text = _dataItem.Descricao
            End If

        ElseIf e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).Text = "ID"
            e.Row.Cells(1).Text = "Requisito"
        ElseIf e.Row.RowType = DataControlRowType.Footer Then

        ElseIf e.Row.RowType = DataControlRowType.EmptyDataRow Then

        End If

    End Sub

    Protected Sub DataBindItemSelecionado()

        Throw New NotImplementedException
        'If Me.ItemSelecionado Is Nothing Then
        '    TxEuComo.Text = Nothing
        '    TxPara.Text = Nothing
        '    TxQuero.Text = Nothing
        'Else
        '    TxEuComo.Text = Me.ItemSelecionado.EuComo
        '    TxPara.Text = Me.ItemSelecionado.Para
        '    TxQuero.Text = Me.ItemSelecionado.Quero
        'End If

    End Sub

#End Region

#Region "Operacoes do Usuario"

    Private Sub btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click
        Me.CadastrarItemBacklog()
    End Sub

    Private Sub BtSalvar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtSalvar.Click
        Throw New NotImplementedException
    End Sub

    Private Sub BtAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtAlterar.Click
        Throw New NotImplementedException
    End Sub

    Protected Sub SolicitaEdicao(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.IndexGdRequisitoEditar = CInt(sender.CommandArgument)
    End Sub

    Protected Sub SairEdicao(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.IndexGdRequisitoEditar = -1
    End Sub

    Protected Sub AlterarRequisito(ByVal sender As Button, ByVal e As System.EventArgs)
        Dim _requisito As Requisito
        Dim _id As Long
        Dim _row As GridViewRow = GdRequisitos.Rows(Me.IndexGdRequisitoEditar)
        Dim TxtReqAlterar As TextBox = CType(_row.FindControl("TxtReqAlterar"), TextBox)
        Dim _descricao As String

        _descricao = TxtReqAlterar.Text
        _id = CLng(sender.CommandArgument)
        _requisito = Requisito.BuscarPorId(_id)

        _requisito.AlterarDescricao(_descricao)
        Me.IndexGdRequisitoEditar = -1
    End Sub

#End Region

#Region "Classes Auxiliares"

#End Region

    Protected Sub CadastrarItemBacklog()

        Dim _item As ItemBackLog
        _item = New ItemBackLog

        With _item
            .Titulo = "Integrando com TELA"
            .EuComo = "Leonardo"
            .Quero = "Provar que a integração TELA camada de NEGOCIO funciona"
            .Para = "Dar continuidade aos trabalhos de desenvolvimento de framework rico"
            .Prioridade = 10
        End With

        _item.Salvar()

    End Sub



End Class